﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace luanvanFinal
{
    /// <summary>
    /// Interaction logic for wdStageEdit.xaml
    /// </summary>
    public partial class wdStageEdit : Window
    {
        private int idProject;
        private String stageTitle;
        public wdStageEdit()
        {
            InitializeComponent();
        }
        public int IdProject
        {
            get { return idProject; }
            set { idProject = value; }
        }
        public String StageTitle
        {
            get { return stageTitle; }
            set { stageTitle = value; }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            ///////////////////////////////////////////////////
            //Update
            Boolean checkTitle = false;
            clsStage stage = new clsStage();
            stage.IdProject = idProject;
            stage.Title = tbTitle.Text;
            stage.StartDay = Convert.ToDateTime(dpStartDay.Text);
            stage.EndDay = Convert.ToDateTime(dpEndDay.Text);
            stage.Description = new TextRange(rtbDes.Document.ContentStart, rtbDes.Document.ContentEnd).Text;
            stage.TotalPercent = 0;
            clsProject project = new clsProject();
            String SQLGetStartDay = "select proStartDay from project where ID = '" + idProject + "'";
            DataTable dt = project.getDataProject(SQLGetStartDay);
            clsStage cStage = new clsStage();
            String SQLCheckStage = "select title from stage where idProject = '" + idProject + "'";
            DataTable dtStage = cStage.getDataStage(SQLCheckStage);
            for (int i = 0; i < dtStage.Rows.Count; i++)
            {
                if (dtStage.Rows[i][0].ToString() == tbTitle.Text)
                    checkTitle = true;
            }
            if (Convert.ToDateTime(stage.EndDay) < Convert.ToDateTime(stage.StartDay))
            {
                MessageBox.Show("End Day must be larger than Start Day", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (Convert.ToDateTime(stage.StartDay) < Convert.ToDateTime(dt.Rows[0][0].ToString()))
            {
                MessageBox.Show("Start Day of Stage must be larger than Start Day of Project", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (checkTitle == true)
            {
                MessageBox.Show("You had this Title in this Project ", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                try
                {
                    stage.updateStage();
                    checkTitle = false;
                    MessageBox.Show("You have edit Stage success !!!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                catch
                {
                }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public void loadForm()
        {
            clsProject project1 = new clsProject();
            tbProject.Text = project1.getNameFromId(idProject);
            String SQL = "select * from stage where idProject='"+idProject+"'and title = '"+stageTitle+"'";
            clsStage stage1 = new clsStage();
            DataTable dt1= stage1.getDataStage(SQL);
            tbTitle.Text = stageTitle;
            dpStartDay.Text = dt1.Rows[0][3].ToString();
            dpEndDay.Text = dt1.Rows[0][4].ToString();
            Paragraph para = new Paragraph();
            para.Inlines.Add(new Run(dt1.Rows[0][5].ToString()));
            FlowDocument fd = new FlowDocument(para);
            rtbDes.Document = fd;
            tbTitle.IsEnabled = false;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loadForm();
        }
    }
}
